package com.example.java_utils.utils;

import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    //获取当前日期
    public static String getCurrDate() {
        LocalDate today = LocalDate.now();
        return today.toString();
    }

    //获取年月日
    public static void getDetailDate() {
        LocalDate today = LocalDate.now();
        int year = today.getYear();
        int month = today.getMonthValue();
        int day = today.getDayOfMonth();
        System.out.printf("Year:%d Month:%d Day: %d \n", year, month, day);
    }

    //处理特定日期
    public static LocalDate generateDate(int year, int month, int day) {
        return LocalDate.of(year, month, day);
    }

    //判断两个日期是否相等
    public static boolean compareDate() {
        LocalDate today = LocalDate.now();
        LocalDate localDate = LocalDate.of(2021, 7, 5);
        if (today.equals(localDate)) {
            System.out.printf("Today %s and localdate %s are same\n", today, localDate);
        }
        return today.equals(localDate);
    }

    //处理周期性日期
    public static void cycleDate() {
        LocalDate today = LocalDate.now();
        LocalDate dateOfBirth = LocalDate.of(2021, 7, 5);
        MonthDay birthday = MonthDay.of(dateOfBirth.getMonth(), dateOfBirth.getDayOfMonth());
        MonthDay currentMonthDay = MonthDay.from(today);
        if (currentMonthDay.equals(birthday)) {
            System.out.println("Today is your birthday!");
        } else {
            System.out.println("today is not your birthday!");
        }
    }

    //获取当前时间
    public static LocalTime getCurrTime() {
        LocalTime localTime = LocalTime.now();
        System.out.println("current time is " + localTime);
        return localTime;
    }

    //计算几个小时后的日期
    public static LocalTime getTimeAfterHours(LocalTime time,int hoursAfter){
        LocalTime timeAfterHours = time.plusHours(hoursAfter);
        System.out.println(timeAfterHours);
        return timeAfterHours;
    }

    //获取一周后的日期
    public static LocalDate getNextWeekDate(LocalDate localDate){
        LocalDate nextWeekDate = localDate.plusWeeks(1);
        System.out.println(nextWeekDate);
        return nextWeekDate;
    }

    //获取一周后的日期
    public static LocalDate getNextYearDate(LocalDate localDate){
        LocalDate nextYearDate = localDate.plusYears(1);
        System.out.println(nextYearDate);
        return nextYearDate;
    }

    public static void clock(){
        var clock = Clock.systemUTC();
        System.out.println("Clock:"+clock);
        System.out.println("Clock:"+clock.millis());
        var deafaultClock  = Clock.systemDefaultZone();
        System.out.println("Clock:"+deafaultClock);
    }

    public  static void isBeforeOrAfter(){
        LocalDate today = LocalDate.now();
        LocalDate tomorrow = today.plusDays(1);
        LocalDate yesterday = today.minusDays(1);
        assert tomorrow.isAfter(today);
        assert today.isBefore(yesterday);
        assert tomorrow.isBefore(today);
        assert yesterday.isAfter(today);
    }

    public static void getZoneTime(){
        ZoneId americaZone = ZoneId.of("America/New_York");
        LocalDateTime localDateTime = LocalDateTime.now();
        ZonedDateTime zonedDateTimeInNewyork = ZonedDateTime.of(localDateTime,americaZone);
        System.out.println(zonedDateTimeInNewyork);
    }
    //检查闰年
    public static boolean isLeapYear(LocalDate localDate){
        return localDate.isLeapYear();
    }

    //预定义的格式化工具
    public static void formatDate(){
        String day = "20210705";
        LocalDate formatted = LocalDate.parse(day, DateTimeFormatter.BASIC_ISO_DATE);
        System.out.println(formatted);
    }
    public static void main(String[] args) {
        getDetailDate();
        System.out.println(generateDate(2011, 12, 31));
        //错误输入
//        System.out.println(generateDate(2011,13,31));
        compareDate();
        cycleDate();
        getCurrTime();
        getTimeAfterHours(LocalTime.now(),2);
        getNextWeekDate(LocalDate.now());
        getNextYearDate(LocalDate.now());
        clock();
        isBeforeOrAfter();
        System.out.println();
        getZoneTime();
        isLeapYear(LocalDate.now());
        formatDate();
    }
}
